import axios from "axios";
// import { bus } from "../main.js";
// import store from "../vuex/store";
import { randomFn8, obj2str, createTimeStamp, md5, encryptByDES } from './encryption'

axios.defaults.timeout = 100000;
axios.interceptors.request.use((config) => {
  console.log('111=>')
  console.log(config.data)
  //debugger
  try{
  const systemCode = 'S08SU03'
  let data = {} //参数
  let noneData = {
    hasParms: false,
    hasSign: true
  }
  // get请求的话取到url上面的参数
  if (config.method === 'get' || config.method === 'delete') {
    // 可能是url传参
    if (config.url.indexOf("?") !== -1) {
      data = formatUrlData(config.url)
    } else {
      data = config.params
    }
  } else {
    if (config.url.indexOf("?") !== -1) {
      data = formatUrlData(config.url)
    } else {
      //config.data = JSON.parse(JSON.stringify(data).toString().replace("\\\\",""))  //去掉参数中的/
      data = config.data
    }
  }
  // 判断没有参数的行为
  if (data == undefined || data == null || JSON.stringify(data) == "{}"||data.length== 0) {
    data = noneData
  }
  console.log('>>>>>>>>>>>>')
  console.log(data)
   //data = JSON.parse(JSON.stringify(data).toString().replace(/\//ig, ''))  //去掉参数中的\""
   //debugger
    data = JSON.parse(JSON.stringify(data).toString().replace(/\\":undefined/ig, '\\":\\"\\"'))  //去掉参数中的undefined
    data = JSON.parse(JSON.stringify(data).toString().replace(/\\":NaN/ig, '\\":\\"\\"'))  //去掉参数中的NaN
    data = JSON.parse(JSON.stringify(data).toString().replace(/\\":null/ig, '\\":\\"\\"'))  //去掉参数中的null
    data = JSON.parse(JSON.stringify(data).toString().replace(/undefined/ig, '""'))  //去掉参数中的undefined
    data = JSON.parse(JSON.stringify(data).toString().replace(/NaN/ig, '""'))  //去掉参数中的NaN
    data = JSON.parse(JSON.stringify(data).toString().replace(/null/ig, '""'))  //去掉参数中的null
   
  // data = data.replace(reg,'');
  console.log(data)
  console.log('去掉data中的undefined', data)
  let t1 = config.headers.t1 || createTimeStamp()  //不能覆盖请求头中已经存在的t1
  let t2 = randomFn8()
  let token = ''
  // token = localStorage.getItem('kd_token')?token:''
  // if (store.state.hasLogin === true) {
  //   token = localStorage.getItem('kd_token').replace('"', '').replace('"', '')
  // }
  let str = md5(`[${obj2str(data)}]@systemcode=${systemCode}&t1=${t1}&t2=${t2}`)
  console.log('排序后的值', `[${obj2str(data)}]@systemcode=${systemCode}&t1=${t1}&t2=${t2}`)
  console.log('md5后的值', str)
  // 避免覆盖之前的
  config.headers["system"] = systemCode;
  config.headers["token"] = token;
  config.headers["sign"] = str;
  config.headers["t1"] = t1;
  config.headers["t2"] = t2;
  return config;
}catch(err){
  console.log('111222=>',err)
}
}, error => {
  console.log('111222=>',error)
  return error;
});



axios.interceptors.response.use(
  response => {
    if (response.headers["content-type"].indexOf('msexcel') === -1) {
      if (response.data) {
        if (response.data.msg == 'Token已失效' || response.data.code == 'FS05SU01501' || response.data.code == 'E040110000001' || response.data.code == 'F03003501' || response.data.code == 'F03003500' || response.data.code == 'FS05SU01500' || response.data.code == 'FS03SU02500' || response.data.code == 'FS03SU02501'|| response.data.code == 'FS03SU01401') {
         
        }
      }
    }
    return response;
  }, error => {
    return error;
  }
);

function formatUrlData(value) {
  // 1. 监测是否传递参数
  if (value.indexOf("?") === -1) {
    return {}
  }
  let str = value.split("?")[1];

  let attr = {};
  // 空值检测 错误检测
  if (!str || str.indexOf("=") === -1) {
    return attr;
  }
  // 1. 首先检测是否存在 & 符号 如果没有
  if (str.indexOf("&") === -1) {
    attr[str.split("=")[0]] = str.split("=")[1];
  } else {
    let arr = str.split("&");
    arr.forEach((v, index) => {
      attr[v.split("=")[0]] = v.split("=")[1];
    });
  }
  return attr;
}

function closeSystem(){
//   bus.$emit("showNotice", {
//     content: `身份令牌失效，请重新登录`,
//     type: "abnormal",
//   }),
//   localStorage.removeItem("kd_deptinfo");
//   localStorage.removeItem("kd_token");
//   localStorage.removeItem("kd_info");
//   // 关闭窗口
  // window.close();   
}

// function filterParams(obj) {
//   var _newPar = {};
//   for (var key in obj) {
//       if (obj[key].replace(/(^\s*)|(\s*$)/g, '') !== null || obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== undefined) {
//           _newPar[key] = obj[key];
//       }
//   }
//   return _newPar;
// }




export default axios;
